###############################################################################
#################04. Final Data Manipulation and Saving Data set###############
###############################################################################

#Standardize variables to ease interpretation#

#Income#
pols$scale_ggini_inc1 <- c(scale(pols$ggini_inc1))
pols$scale_ggini_inc2 <- c(scale(pols$ggini_inc2))
pols$scale_ggini_inc3 <- c(scale(pols$ggini_inc3))
pols$scale_ggini_inc4 <- c(scale(pols$ggini_inc4))
pols$scale_ggini_inc5 <- c(scale(pols$ggini_inc5))
pols$scale_gtheil_inc1 <- c(scale(pols$gtheil_inc1))
pols$scale_gtheil_inc2 <- c(scale(pols$gtheil_inc2))
pols$scale_gtheil_inc3 <- c(scale(pols$gtheil_inc3))
pols$scale_gtheil_inc4 <- c(scale(pols$gtheil_inc4))
pols$scale_gtheil_inc5 <- c(scale(pols$gtheil_inc5))
pols$scale_gcov_inc1 <- c(scale(pols$gcov_inc1))
pols$scale_gcov_inc2 <- c(scale(pols$gcov_inc2))
pols$scale_gcov_inc3 <- c(scale(pols$gcov_inc3))
pols$scale_gcov_inc4 <- c(scale(pols$gcov_inc4))
pols$scale_gcov_inc5 <- c(scale(pols$gcov_inc5))
pols$scale_sdmw_inc1 <- c(scale(pols$sdmw_inc1))
pols$scale_sdmw_inc2 <- c(scale(pols$sdmw_inc2))
pols$scale_sdmw_inc3 <- c(scale(pols$sdmw_inc3))
pols$scale_sdmw_inc4 <- c(scale(pols$sdmw_inc4))
pols$scale_sdmw_inc5 <- c(scale(pols$sdmw_inc5))
pols$scale_ggini_inc_cs <- c(scale(pols$ggini_inc_cs))
pols$scale_gtheil_inc_cs <- c(scale(pols$gtheil_inc_cs))
pols$scale_gcov_inc_cs <- c(scale(pols$gcov_inc_cs))
pols$scale_sdmw_inc_cs <- c(scale(pols$sdmw_inc_cs))

#Other continous variables#


pols$scale_socioeconomicsalw <- c(scale(pols$socioeconomicsalw))
pols$scale_socioeconomicsal <- c(scale(pols$socioeconomicsal))
pols$scale_dispgini <- c(scale(pols$gini_disp))
pols$scale_mgini <- c(scale(pols$gini_mkt))
pols$scale_enep <- c(scale(pols$enep))
pols$scale_ud <- c(scale(pols$ud_int))
pols$scale_vturn <- c(scale(pols$vturn))
pols$scale_gdp <- c(scale(pols$realgdpgr))
pols$scale_inflation <- c(scale(pols$inflation))
pols$scale_unemp <- c(scale(pols$unemp))
pols$scale_mag <- c(scale(pols$logmag))

#Dependent variables

pols$scale_econspan <- c(scale(pols$econspan))
pols$scale_galtanspan <- c(scale(pols$galtanspan))
pols$scale_econsdw <- c(scale(pols$econsdw))
pols$scale_galtansdw <- c(scale(pols$galtansdw))
pols$scale_econpiw <- c(scale(pols$econpiw))
pols$scale_galtanpiw <- c(scale(pols$galtanpiw))

#Time#

pols <- pols %>%
  dplyr::group_by(country.name) %>%
  dplyr::mutate(countryyear = electionyear - min(electionyear) + 1) %>%
  dplyr::mutate(order = rank(electionyear))

a <- which(pols$country.name == "Croatia"  |
             pols$country.name == "Bulgaria" | 
             pols$country.name == "Latvia")

pols <- pols[-a,]

#Create lagged partisan sorting variable#

pols <- pols %>% ungroup()

pols$inc1_lag <- c(NA, pols$scale_ggini_inc1[-nrow(pols)])
pols$inc1_lag[which(!duplicated(pols$country.name))] <- NA

pols$inc2_lag <- c(NA, pols$scale_ggini_inc2[-nrow(pols)])
pols$inc2_lag[which(!duplicated(pols$country.name))] <- NA

pols$inc3_lag <- c(NA, pols$scale_ggini_inc3[-nrow(pols)])
pols$inc3_lag[which(!duplicated(pols$country.name))] <- NA

pols$inc4_lag <- c(NA, pols$scale_ggini_inc4[-nrow(pols)])
pols$inc4_lag[which(!duplicated(pols$country.name))] <- NA

pols$inc5_lag <- c(NA, pols$scale_ggini_inc5[-nrow(pols)])
pols$inc5_lag[which(!duplicated(pols$country.name))] <- NA

pols$disp_lag <- c(NA, pols$scale_dispgini[-nrow(pols)])
pols$disp_lag[which(!duplicated(pols$country.name))] <- NA

pols$pol_lag <- c(NA, pols$scale_econsdw[-nrow(pols)])
pols$pol_lag[which(!duplicated(pols$country.name))] <- NA

#####Load plm package#####

library(plm)
library(pcse)
library(lmtest)
library(dplyr)

pols <- pdata.frame(pols, index = c("country.name", "order"))

###Save the Data For Making Tables and Figures###

saveRDS(pols, 'pols_bjps')